package com.selftool.gui.devframe;

import com.selftool.Main;
import com.selftool.log.Logger;
import com.selftool.service.JDBCService;
import org.springframework.stereotype.Component;

import javax.swing.table.AbstractTableModel;
import java.util.Vector;

/**
 * @program: selftool
 * @description: 研发管理表格数据模型
 * @author: CYQ
 * @create: 2024-02-27 10:13
 **/
@Component
public class TableModel extends AbstractTableModel {
    private Vector titlesV = new Vector();//存储标题
    public static String[] titel = new String[]{"补丁组", "补丁名称", "安装时间", "备注"};
    private static Vector<Vector> dataV = new Vector<>();//存储数据
    public TableModel(String[] titles){
        titlesV = new Vector();
        dataV = new Vector();
        for (int i = 0; i < titles.length; i++) {
            titlesV.add(titles[i]);
        }
    }
    public TableModel(){

    }
    public void setTitles(String[] titles){
        titlesV = new Vector();
        dataV = new Vector();
        for (int i = 0; i < titles.length; i++) {
            titlesV.add(titles[i]);
        }
    }
//    public TableModel(NCVO vo){
//        String[] titles = vo.getAttributeNames();
//        titlesV = new Vector();
//        dataV = new Vector();
//        for (int i = 0; i < titles.length; i++) {
//            titlesV.add(titles[i]);
//        }
//    }
    public void addData(String[] data){
        Vector t = new Vector<>();
        for (int j = 0; j < data.length; j++) {
            t.add(data[j]);
        }
        dataV.add(t);
    }
    public void addDatas(String[][] data){
        for (int i = 0; i < data.length; i++) {
            Vector t = new Vector<>();
            for (int j = 0; j < data[i].length; j++) {
                t.add(data[i][j]);
            }
            dataV.add(t);
        }
    }
    @Override
    public int getRowCount() {
        return dataV.size();
    }

    @Override
    public int getColumnCount() {
        return titlesV.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return dataV.get(rowIndex).get(columnIndex);
    }

    @Override
    public String getColumnName(int column) {
        return (String) titlesV.get(column);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }
    /**
    * @Description: 获取数据模型，模型结构为{"id":{"code","id","name":"主键","value":"test"}}
    * @Param: []
    * @return: java.util.Map
    * @Author: CYQ
    * @Date: 2024/2/29
    */
//    private Map dataModel(){
//        List<ModelVO> vo = JDBCUtil.getModel("bd_patch");
//        return null;
//    }
    /**
    * @Description: 刷新模型
    * @Param: []
    * @return: void
    * @Author: CYQ
    * @Date: 2024/3/1
    */
    public static void refresh(){
        dataV.clear();
        JDBCService server = Main.context.getBean(JDBCService.class);
        String[][] data = server.getModel(titel,"bd_patch");
        for (int i = 0; i < data.length; i++) {
            Vector t = new Vector<>();
            for (int j = 0; j < data[i].length; j++) {
                t.add(data[i][j]);
            }
            dataV.add(t);
        }
        Logger.info(data);
    }
}
